package cn.ac.iscas.action.patent;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import cn.ac.iscas.bean.patent.PatentPicture;
import cn.ac.iscas.bean.patent.Patent;
import cn.ac.iscas.bean.project.Project;
import cn.ac.iscas.service.patent.IPatentPictureService;
import cn.ac.iscas.service.patent.IPatentServive;
import cn.ac.iscas.service.project.IProjectService;

@Controller @Scope("prototype")
public class PatentManageAction extends ActionSupport{

	
	/**
	 *处理项目的录入，保存，修改，删除的功能
	 */	
	@Resource private IPatentServive patentService;//运行时动态注入projectService层
	@Resource private IPatentPictureService patentPictureService;//运行时动态注入projectDocumentService层
	
	private Patent patent;//接收处理时动态注入
	private PatentPicture picture;//接收处理时动态注入
	@Resource private IProjectService projectService;//运行时动态注入projectService层
	private Project project;
	/**
	 *上传时动态接收注入的文档文件
	 **/
	private File[] uploadPicture;
	
	/**
	 *上传时动态接收注入的文档文件名
	 **/
	private String[] uploadPictureFileName;
	
	
	/**
	 * 处理项目的详细查询
	 * */
	public String detailQuery()//详细信息查询
	{
		patent = patentService.get(Patent.class, patent.getId());
		return "detailQuery";//跳转到详细信息界面
	}
	/**
	 * 处理项目的修改
	 * */
	public String edit()//项目信息修改
	{
		patent = patentService.get(Patent.class, patent.getId());
		return "edit";//跳转修改界面
	}
	/**
	 * 处理项目的更新
	 * */
	
	/**
	 * 
	 * 项目保存
	 * */
	public String save() throws IOException
	{
		if(project!=null)
		{
		  project = projectService.get(Project.class, project.getId());
		}
		if(patent!=null) System.err.println(patent.getCertificateDate());
		String realpath = ServletActionContext.getServletContext().getRealPath("/patentPictures");
		Set<PatentPicture> patentUploadPictures = new HashSet<PatentPicture>();
		
		if(uploadPicture != null)
		{
			File savedir = new File(realpath);
			if(!savedir.exists()) savedir.mkdir();
			for(int i=0; i<uploadPicture.length;i++)
			{
				PatentPicture picInfo = new PatentPicture();				
				String realName = UUID.randomUUID().toString()+ getExt(uploadPictureFileName[i]);		
				File savefile = new File(savedir , realName);
				FileUtils.copyFile(uploadPicture[i], savefile);
				picInfo.setPictureName(uploadPictureFileName[i]);					
				picInfo.setPath(realpath);
				picInfo.setRealName(realName);
				picInfo.setUploadTime(new Date());
				picInfo.setPatent(patent);
				patentUploadPictures.add(picInfo);
			}
			System.out.println("上传图片文档成功");
			System.out.println(realpath);
		}
		patent.setProject(project);
		patentService.save(patent);
		for(PatentPicture doc:patentUploadPictures)
		{
			patentPictureService.save(doc);
			System.out.println("保存uploadingdoc成功");
		}		
		return "success";
	}	

	public String deletePicture() throws IOException
	{
		picture = patentPictureService.get(PatentPicture.class, picture.getId());
		patentPictureService.delete(picture);
		ActionContext ctx = ActionContext.getContext();     
	    HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
	    response.setContentType("text/html;charset=utf-8");
	    PrintWriter out=response.getWriter();
	    response.setContentType("text/html;charset=utf-8");
	    out.println("成功删除");
		return null;
	}

	public String update() throws IOException
	{
		//得到原有项目文档
		Set<PatentPicture> pictures = patentService.get(Patent.class, patent.getId()).getPictures();
		if(pictures == null)
			pictures = new HashSet<PatentPicture>();
		for(PatentPicture doc:pictures)//更新原有的项目文档
		{
			doc.setPatent(patent);
		}
		String realpath = ServletActionContext.getServletContext().getRealPath("/patentPictures");
		//处理已结题的文档更新
		if(uploadPicture != null)
		{
			File savedir = new File(realpath);
			if(!savedir.exists()) savedir.mkdir();
			for(int i=0; i<uploadPicture.length;i++)
			{
				PatentPicture picInfo = new PatentPicture();				
				String realName = UUID.randomUUID().toString()+ getExt(uploadPictureFileName[i]);		
				File savefile = new File(savedir , realName);
				FileUtils.copyFile(uploadPicture[i], savefile);
				picInfo.setPictureName(uploadPictureFileName[i]);	
				picInfo.setRealName(realName);
				picInfo.setUploadTime(new Date());
				picInfo.setPatent(patent);
				pictures.add(picInfo);
			}
		}
		//得到原有专利
		if(project.getId() != 0)
		{
			project = projectService.get(Project.class, project.getId());
		}
		else
		{
			Project oldProject = patentService.get(Patent.class, patent.getId()).getProject();
			project = oldProject;
		}
		
		//将项目更新导数据库中
		patent.setProject(project);
		patentService.update(patent);
		for(PatentPicture pic:pictures)
		{
			patentPictureService.save(pic);//保存原有的项目文档
		}
		
		return "success";
	}
	public String delete() throws IOException
	{
		patent = patentService.get(Patent.class,patent.getId());
		patentService.delete(patent);
		ActionContext ctx = ActionContext.getContext();     
	    HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
	    response.setContentType("text/html;charset=utf-8");
	    PrintWriter out=response.getWriter();
	    response.setContentType("text/html;charset=utf-8");
	    out.println("成功删除");
		return null;
	}
	public static String getExt(String fileName)
	{
		return fileName.substring(fileName.lastIndexOf("."));
	}
	public IPatentServive getPatentService() {
		return patentService;
	}
	public void setPatentService(IPatentServive patentService) {
		this.patentService = patentService;
	}
	public IPatentPictureService getPatentPictureService() {
		return patentPictureService;
	}
	public void setPatentPictureService(IPatentPictureService patentPictureService) {
		this.patentPictureService = patentPictureService;
	}
	public Patent getPatent() {
		return patent;
	}
	public void setPatent(Patent patent) {
		this.patent = patent;
	}
	public PatentPicture getPicture() {
		return picture;
	}
	public void setPicture(PatentPicture picture) {
		this.picture = picture;
	}
	public File[] getUploadPicture() {
		return uploadPicture;
	}
	public void setUploadPicture(File[] uploadPicture) {
		this.uploadPicture = uploadPicture;
	}
	public String[] getUploadPictureFileName() {
		return uploadPictureFileName;
	}
	public void setUploadPictureFileName(String[] uploadPictureFileName) {
		this.uploadPictureFileName = uploadPictureFileName;
	}
	public void setProjectService(IProjectService projectService) {
		this.projectService = projectService;
	}
	public IProjectService getProjectService() {
		return projectService;
	}
	public void setProject(Project project) {
		this.project = project;
	}
	public Project getProject() {
		return project;
	}

}
